package main

import "fmt"

func main() {
	nums := []int{1, 1, 1, 2, 2, 3}
	v := removeDuplicatesII(nums)
	fmt.Println(v)
	fmt.Println(nums)
}

// 删除有序数组中的重复项 II
// https://leetcode.cn/problems/remove-duplicates-from-sorted-array-ii/
func removeDuplicatesII(nums []int) int {
	l := len(nums)
	pos := 0
	times := 1
	for i := 1; i < l; i++ {
		if nums[i] == nums[pos] {
			times++
			if times > 2 {
				continue
			}
		} else {
			times = 1

		}

		nums[pos+1] = nums[i]
		pos++
	}
	return pos + 1
}
